package com.scs.application.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.exception.BusinessException;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.modules.sys.entity.AppraiseStandard;
import com.scs.application.modules.sys.mapper.AppraiseStandardMapper;
import com.scs.application.modules.sys.service.AppraiseStandardService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 */
@Service
public class AppraiseStandardServiceImpl extends BaseServiceImpl<AppraiseStandardMapper, AppraiseStandard> implements AppraiseStandardService {
    @Override
    public boolean saveOrUpdate(AppraiseStandard entity) {
        if (entity.getAppraiseLow() > entity.getAppraiseHigh()) {
            throw new BusinessException("下限不能高于上限");
        }
        List<AppraiseStandard> appraiseStandards = list(Wrappers.<AppraiseStandard>query()
                .eq("appraise_type", entity.getAppraiseType())
                .ne(Strings.isNotBlank(entity.getId()), "id", entity.getId())
                .lt("appraise_low", entity.getAppraiseHigh())
                .gt("appraise_high", entity.getAppraiseLow()));
        if (appraiseStandards.size() > 0) {
            throw new BusinessException("存在区间冲突，不能保存");
        }
        return super.saveOrUpdate(entity);
    }
}
